package com.dayaokeji.imkitwrapper.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.beetle.bauhinia.gallery.GalleryImage;
import com.beetle.bauhinia.gallery.ui.GalleryAdapter;
import com.beetle.bauhinia.gallery.ui.GalleryGridUI;
import com.beetle.bauhinia.gallery.ui.GalleryUI;
import com.beetle.bauhinia.gallery.view.ScrollViewPager;
import com.dayaokeji.imkitwrapper.R;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class GalleryFragment extends BaseFragment {

    private static final String INTENT_EXTRA_KEY_POSITION = "position";
    private static final String INTENT_EXTRA_KEY_IMAGES = "images";
    private static final String INTENT_EXTRA_KEY_IS_ENTER_FROM_GRID = "is_enter_from_grid";

    private ScrollViewPager mViewPager;
    private GalleryAdapter mPagerAdapter;
    private ImageButton ibViewMorePicture;

    private int mPosition;
    private int mTotal;
    private ArrayList<GalleryImage> imagesList;
    private boolean isEnterFromGrid = false;
    private OnNavigationViewMoreListener onNavigationViewMoreListener;

    public static GalleryFragment newInstance(ArrayList<GalleryImage> galleryImages, int position) {

        Bundle args = new Bundle();
        args.putParcelableArrayList(INTENT_EXTRA_KEY_IMAGES, galleryImages);
        args.putInt(INTENT_EXTRA_KEY_POSITION, position);
        GalleryFragment fragment = new GalleryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_gallery, container, false);
        mViewPager = (ScrollViewPager) rootView.findViewById(com.beetle.imkit.R.id.imagebrowser_svp_pager);
        ibViewMorePicture = (ImageButton) rootView.findViewById(com.beetle.imkit.R.id.ib_view_more_picture);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    private void init() {
        mPosition = getArguments().getInt(INTENT_EXTRA_KEY_POSITION, 0);
        imagesList = getArguments().getParcelableArrayList(INTENT_EXTRA_KEY_IMAGES);
        mTotal = imagesList.size();
        if (mPosition > mTotal) {
            mPosition = mTotal - 1;
        }
        mPagerAdapter = new GalleryAdapter(getActivity(), imagesList);
        mPagerAdapter.setOnItemClickListener(new GalleryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup container, View view, int position) {
                getActivity().finish();
            }
        });
        mViewPager.setAdapter(mPagerAdapter);
        if (mTotal > 0) {
            mViewPager.setCurrentItem(mPosition, false);
        }
        if (isEnterFromGrid) {
            hideViewMorePictureButton();
        } else {
            showViewMorePictureButton();
        }
    }

    private void hideViewMorePictureButton() {
        ibViewMorePicture.setVisibility(View.GONE);
    }

    private void showViewMorePictureButton() {
        ibViewMorePicture.setVisibility(View.VISIBLE);
        ibViewMorePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateToViewMorePicture();
            }
        });
    }

    private void navigateToViewMorePicture() {
//        Intent intent = GalleryGridUI.getCallingIntent(getActivity(), imagesList, mViewPager.getCurrentItem());
//        startActivity(intent);
        if (onNavigationViewMoreListener != null) {
            onNavigationViewMoreListener.viewMorePicture(imagesList, mViewPager.getCurrentItem());
        }
    }

    public void setOnNavigationViewMoreListener(OnNavigationViewMoreListener onNavigationViewMoreListener) {
        this.onNavigationViewMoreListener = onNavigationViewMoreListener;
    }

    public interface OnNavigationViewMoreListener{
        void viewMorePicture(ArrayList<GalleryImage> imagesList,int position);
    }

}
